package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.Question;

public class QuestionDao extends DaoUtil {
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null;
	//添加问题
	public boolean add(Question question){
		//String sql = "insert into question(courseID,askPerson,quesContent,answer,createTime,answerTime) values(?,?,?,?,?,?)";
		String sql = "insert into question(courseID,askPerson,quesContent,answer,ansPerson,createTime,answerTime) values(?,?,?,?,?,?,?)";
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, question.getCourseID());
			stat.setString(2, question.getAskPerson());
			stat.setString(3, question.getQuesContent());
			stat.setString(4,question.getAnswer());
			stat.setString(5,question.getAnsPerson());
			System.out.println(question.getAnsPerson());
			stat.setString(6, question.getCreateTime());
			stat.setString(7, question.getAnswerTime());
			stat.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			closed();
		}
		
		
		return true;
	}
	
	//取得问题列表
	public List<Question> getQuestionList(){
		List<Question> questionList = new ArrayList<Question>();
		String sql = "select * from question";
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while(rs.next()){
				Question question = new Question();
				question.setQuestionID(rs.getInt("questionID"));
				question.setCourseID(rs.getInt("courseID"));
				question.setAskPerson(rs.getString("askPerson"));
				question.setQuesContent(rs.getString("quesContent"));
				question.setAnswer(rs.getString("answer"));
				question.setAnsPerson(rs.getString("ansPerson"));
				//System.out.println("questionDaogetQuestonlist");
				//System.out.println(rs.getString("ansPerson"));
				question.setCreateTime(rs.getString("createTime"));
				question.setAnswerTime(rs.getString("answerTime"));
				questionList.add(question);
				
			}
			return questionList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally{
			closed();
		}
		
	}
	
	//回答问题
	public int answerQuestion(Question question){
		String sql = "update question set answer = ? ,answerTime = ? where questionID = ?";
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1, question.getAnswer());
			stat.setString(2, question.getAnswerTime());
			stat.setInt(3, question.getQuestionID());
			return stat.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		
		
	}
	
}
